#extension GL_EXT_gpu_shader4 : enable

varying out int faceIdOut;

uniform sampler2D deltaSampler;
uniform sampler2D weightSampler;
uniform int deltaTopLevel;
uniform int weightTopLevel;
uniform float faceResolution;
uniform float farPlane;
uniform int faceId;

void main(void)
{
	vec3 delta = vec3(texture2D(deltaSampler, vec2(0.5, 0.5), float(deltaTopLevel)).rgb);
	vec3 weight = vec3(texture2D(weightSampler, vec2(0.5, 0.5), float(weightTopLevel)).rgb);

	float deltaScale = length(delta);
	float weightScale = max(length(weight), 0.0005);

	float depth = 1.0/(deltaScale * weightScale * faceResolution);
	if(depth > farPlane)
	{
		discard;
	}

	gl_FragDepth = depth;
	faceIdOut = faceId;
}
